/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.documents.main.action;

import com.documents.common.core.action.BaseActionSupport;
import com.documents.htmlobj.CatTreeObj;
import com.documents.htmlobj.CountryObj;
import com.documents.htmlobj.EnProductObj;
import com.documents.htmlobj.ProductObj;
import com.documents.pojo.Message;
import com.documents.pojo.Notice;
import com.documents.pojo.Product;
import com.documents.pojo.Users;
import com.documents.service.MessageService;
import com.documents.service.NoticeService;
import com.documents.service.country.CountryService;
import com.documents.service.document.DocumentGroupService;
import com.documents.service.document.DocumentIndexTreeService;
import com.documents.service.document.DocumentTypeService;
import com.documents.service.product.ProductService;
import com.documents.service.users.UserService;
import com.opensymphony.xwork2.ActionContext;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;

/**
 *
 * @author Administrator
 */
public class MainAction extends BaseActionSupport implements ServletRequestAware {

    private String username;
    private String password;
    private UserService userService;
    private DocumentTypeService documentTypeService;
    private DocumentGroupService documentGroupService;
    private List<CatTreeObj> catTree;
    private List<ProductObj> proList;
    private DocumentIndexTreeService documentIndexTreeService;
    private List<CountryObj> countryList;
    private List<Product> productList;
    private CountryService countryService;
    private ProductService productService;
    private NoticeService noticeService;
    private MessageService messageService;
    private Users user;
    private HashMap<String, List<EnProductObj>> enMap;
    private String code;
    private List<Notice> nlist;
    private List<Notice> glist;
    private List<Message> mlist;
    private String msg;
    private String mail;

    public String getMail() {
        return mail;
    }

    public void setMail(String mail) {
        this.mail = mail;
    }
    
    

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public MessageService getMessageService() {
        return messageService;
    }

    public void setMessageService(MessageService messageService) {
        this.messageService = messageService;
    }

    public List<Message> getMlist() {
        return mlist;
    }

    public void setMlist(List<Message> mlist) {
        this.mlist = mlist;
    }

    public NoticeService getNoticeService() {
        return noticeService;
    }

    public void setNoticeService(NoticeService noticeService) {
        this.noticeService = noticeService;
    }

    public List<Notice> getGlist() {
        return glist;
    }

    public void setGlist(List<Notice> glist) {
        this.glist = glist;
    }

    public List<Notice> getNlist() {
        return nlist;
    }

    public void setNlist(List<Notice> nlist) {
        this.nlist = nlist;
    }

    public List<CountryObj> getCountryList() {
        return countryList;
    }

    public void setCountryList(List<CountryObj> countryList) {
        this.countryList = countryList;
    }

    public HashMap<String, List<EnProductObj>> getEnMap() {
        return enMap;
    }

    public void setEnMap(HashMap<String, List<EnProductObj>> enMap) {
        this.enMap = enMap;
    }

    public List<ProductObj> getProList() {
        return proList;
    }

    public void setProList(List<ProductObj> proList) {
        this.proList = proList;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public Users getUser() {
        return user;
    }

    public void setUser(Users user) {
        this.user = user;
    }

    public CountryService getCountryService() {
        return countryService;
    }

    public void setCountryService(CountryService countryService) {
        this.countryService = countryService;
    }

    public List<Product> getProductList() {
        return productList;
    }

    public void setProductList(List<Product> productList) {
        this.productList = productList;
    }

    public List<CatTreeObj> getCatTree() {
        return catTree;
    }

    public void setCatTree(List<CatTreeObj> catTree) {
        this.catTree = catTree;
    }

    public DocumentIndexTreeService getDocumentIndexTreeService() {
        return documentIndexTreeService;
    }

    public void setDocumentIndexTreeService(DocumentIndexTreeService documentIndexTreeService) {
        this.documentIndexTreeService = documentIndexTreeService;
    }

    public DocumentGroupService getDocumentGroupService() {
        return documentGroupService;
    }

    public void setDocumentGroupService(DocumentGroupService documentGroupService) {
        this.documentGroupService = documentGroupService;
    }

    public DocumentTypeService getDocumentTypeService() {
        return documentTypeService;
    }

    public void setDocumentTypeService(DocumentTypeService documentTypeService) {
        this.documentTypeService = documentTypeService;
    }

    public UserService getUserService() {
        return userService;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public ProductService getProductService() {
        return productService;
    }

    public void setProductService(ProductService productService) {
        this.productService = productService;
    }

    public String main() throws Exception {
        creatTree();
      //  getAllCountry();
     //   creatEnMap();
      //  creatList();
      //  creatEnMap();
     //   creatNotie();
     //   creatMessage();
        getRequest().getSession().setAttribute("what", "true");
        return "success";
    }

    public String login() {

        String isok = userService.validateLogin(username, password);

        if (isok.equals("1")) {
            creatTree();
            getAllCountry();
            creatEnMap();
            creatList();
        }
        if (isok.endsWith(LOGIN)) {
            msg = "登陆失败！";
        }
        return isok;

    }

    public String adduser() {
        String sessioncode = (String) getRequest().getSession().getAttribute("random");
        if (code.equals(sessioncode)) {
            if (userService.exists("login_name=?", new Object[]{user.getLoginName()})) {
                msg = "会员名重复，请重新填写。";
                this.user = user;
                return INPUT;
            } else {
                user.setRole(1);
                userService.save(user);
                getRequest().getSession().setAttribute("user", user);
                return SUCCESS;
            }

        } else {
            msg = "验证码不正确，请重新填写。";
            this.user = user;
            return INPUT;
        }
    }

    public String logout() throws Exception {
        Map session = ActionContext.getContext().getSession();
        session.clear();
        return "input"; //$NON-NLS-1$
    }

    public void creatTree() {

        catTree = documentIndexTreeService.createTree();

    }

    public void creatList() {
        proList = documentIndexTreeService.createList();
    }

    public void getAllCountry() {

        countryList = countryService.createList();


    }

    public void creatEnMap() {
        enMap = productService.createEn();
    }

    public void creatNotie() {
        nlist = noticeService.getNoticeListTop(1);
        glist = noticeService.getNoticeListTop(2);
    }

    public void creatMessage() {
        mlist = messageService.getMessageListTop(1);
    }

    public String register() {

        return SUCCESS;
    }

    public void containUser() {

        Boolean isok = userService.exists("login_name=?", new Object[]{username});
        ServletActionContext.getResponse().setContentType("text/json; charset=utf-8");
        try {
            ServletActionContext.getResponse().getWriter().write(isok.toString());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
     
    }
        public void containMail() {

        Boolean isok = userService.exists("mail=?", new Object[]{mail});
        ServletActionContext.getResponse().setContentType("text/json; charset=utf-8");
        try {
            ServletActionContext.getResponse().getWriter().write(isok.toString());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
     
    }
    
    
}
